/*
 * ImageCounterServlet.java
 * Written By: Pholyiam T.
 * Tel: 02-715-2293
 * Department/Section: IT-FIS
 * Plant: Teparuk
 * Start Date: 06/21/2007
 *
 * Date        Name         Details                                     Version
 * ----        ----         -------                                     -------
 * mm/dd/yyyy
 * 06/21/2007  Pholyiam T.  Released                                      1.0.0
 */
package com.seagate.app.servlet;

import java.net.URL;
import java.net.URLEncoder;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class ImageCounterServlet extends HttpServlet
{
  private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

  public void init(ServletConfig config) throws ServletException
  {
    super.init(config);
  }

  private byte[] getImageByteArray(String strURL) throws Exception
  {
    URL url = new URL(strURL);
    InputStream in = url.openStream();
    byte[] bImages = new byte[in.available()];

    in.read(bImages);
    in.close();

    return bImages;
  }

  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    response.setContentType("image/jpeg");

    HttpSession session = request.getSession();
    byte[] bImages = (byte[])session.getAttribute("image_counter");
    if(null == bImages) {
      String strAppCounterName = "AMA_STDPART_APP";

      String strURL = "http://ttfisweb2.tep.thai.seagate.com/scripts/useraccess/useraccess.php?application=" +
                      URLEncoder.encode(strAppCounterName, "UTF-8");
//                      URLEncoder.encode("D2D Config WEB Report", "UTF-8");
      try {
        bImages = getImageByteArray(strURL);
      } // try
      catch(Exception e) {
        //
      } // try-catch
    } // if

    session.setAttribute("image_counter", (byte[])bImages);

    OutputStream sos = response.getOutputStream();
    sos.write(bImages);
    sos.flush();
    sos.close();
}

  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
  {
    doGet(request, response);
  }
}